package com.youi.web;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.GroupleaderService;
import com.youi.service.GroupleaderbankService;
import com.youi.service.LeaderprofitService;
import com.youi.service.LeaderprofitdetailService;
import com.youi.service.LeaderstoreService;
import com.youi.service.LeaderwithdrawService;
import com.youi.entity.Groupleader;
import com.youi.entity.Groupleaderbank;
import com.youi.entity.Leaderprofit;
import com.youi.entity.Leaderprofitdetail;
import com.youi.entity.Leaderstore;
import com.youi.entity.Leaderwithdraw;
import com.youi.entity.User;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JedisUtils;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;
import com.youi.util.SmsUtil;

/**
* 团长提现申请控制层
* @author Administrator
*/
@Controller
public class LeaderwithdrawController  extends HttpRequestManage{

		@Autowired
		private LeaderwithdrawService leaderwithdrawService;
		@Autowired
		private GroupleaderService groupleaderService;
		@Autowired
		private GroupleaderbankService groupleaderbankService;
		@Autowired
		private LeaderprofitService leaderprofitService;
		@Autowired
		private LeaderstoreService leaderstoreService;
		@Autowired
		private LeaderprofitdetailService leaderprofitdetailService;

		/**
		 * M-25
		 * 发起提现申请
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 * @throws ParseException
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/leader/addLeaderwithdraw")
		public String addOrEditLeaderwithdraw(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{			
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			DecimalFormat dFormat=new DecimalFormat("0.00");
			String bankName=getString("leaderBankName", request);
			String bankNo=getString("leaderBankNo", request);
//			String name=getString("name", request);
			Double amount=getDouble("amount", request);
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put("leaderId", user.getLeaderId());
			Groupleaderbank groupleaderbank=groupleaderbankService.getByParam(params);
			if (groupleaderbank==null) {
				return JsonUtil.toJson(CommonInfoUtil.NEEDPERMISSIONS, "用户需要完善资料");
			}
			Leaderstore leaderstore=leaderstoreService.getByParam(params);
			if (leaderstore==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "绑定门店信息不存在");
			}
			Leaderprofit leaderprofit=leaderprofitService.getByParam(params);
			if (leaderprofit==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "团长佣金信息不存在");
			}
			if (amount>leaderprofit.getBalance()) {
				return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "输入的金额大于可提现的金额");
			}
			Leaderwithdraw leaderwithdraw=new Leaderwithdraw(user.getLeaderId(),user.getAuthStore(),user.getAuthStoreCode());									
			String orderNo="JXTX"+DateUtil.simpyMd.format(new Date())+SmsUtil.getRandom(4);
			leaderwithdraw.setOrderNo(orderNo);
			leaderwithdraw.setDateNo(DateUtil.simpyMd.format(new Date()));
			leaderwithdraw.setLeaderBankName(bankName);
			leaderwithdraw.setLeaderBankNo(bankNo);
			leaderwithdraw.setLeaderName(groupleaderbank.getRealName());
			leaderwithdraw.setAmount(amount);	
			leaderwithdraw.setBelongStore(leaderstore.getAuthStore());
			leaderwithdraw.setBelongStoreCode(leaderstore.getAuthStoreCode());			
			leaderwithdraw.setLeaderAmount(leaderprofit.getBalance());			
			Double realAnt=ObjectUtil.sciCal(amount-(amount*0.02), 2);
			leaderwithdraw.setRealAnt(realAnt);
			Double serviceCharge=ObjectUtil.sciCal(realAnt*0.076, 2);
			leaderwithdraw.setServiceCharge(serviceCharge);
			leaderwithdraw.setRealityExpend(ObjectUtil.sciCal(realAnt+serviceCharge, 2));
			leaderwithdraw.setCommission(ObjectUtil.sciCal(amount*0.02, 2));	
			leaderwithdraw.setIdCardNo(groupleaderbank.getIdCard());
			leaderprofit.setBalance(ObjectUtil.sciCal(leaderprofit.getBalance()-amount, 2));
			leaderprofit.setWithdres(ObjectUtil.sciCal(leaderprofit.getWithdres()+amount, 2));			
			leaderwithdrawService.saveOrUpdate(leaderwithdraw);
			leaderprofitService.saveOrUpdate(leaderprofit);
			leaderprofitdetailService.saveOrUpdate(new Leaderprofitdetail(leaderstore.getLeaderId(), 1, -amount, "提现支出", orderNo));
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}


		/**
		 *批量删除团长提现申请
		*/
		@ResponseBody
		@RequestMapping("delLeaderwithdrawByIds")
		public String delLeaderwithdrawByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Leaderwithdraw> leaderwithdrawList= leaderwithdrawService.getByParams(params);
			if (leaderwithdrawList==null||leaderwithdrawList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Leaderwithdraw leaderwithdraw : leaderwithdrawList) {
				leaderwithdraw.setDelStatus(DelStatus.DEL);
			}
			leaderwithdrawService.saveOrUpdateAll(leaderwithdrawList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询团长提现申请
		*/
		@ResponseBody
		@RequestMapping("getLeaderwithdraw")
		public String getLeaderwithdraw(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Leaderwithdraw> leaderwithdrawList=leaderwithdrawService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (leaderwithdrawList==null||leaderwithdrawList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=leaderwithdrawService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, leaderwithdrawList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询团长提现申请
		*/
		@ResponseBody
		@RequestMapping("getLeaderwithdrawById")
		public String getLeaderwithdrawById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Leaderwithdraw leaderwithdraw=leaderwithdrawService.get(id);
			if (leaderwithdraw==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderwithdraw);//jsonObject	json对象
		}

		/**
		 * 查询所有团长提现申请
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllLeaderwithdraw")
		public String getAllLeaderwithdraw(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Leaderwithdraw> leaderwithdrawList= leaderwithdrawService.getByParams(null);
			if (leaderwithdrawList==null||leaderwithdrawList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderwithdrawList);//jsonObjectList	json对象集合
		}
}
